package org.dromara.quick.chat.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.web.core.BaseController;
import org.dromara.quick.chat.model.bo.create.AddGroupMemberDTO;
import org.dromara.quick.chat.service.ImGroupMemberService;
import org.springframework.web.bind.annotation.*;

import java.util.concurrent.TimeUnit;


/**
 * @Author: Stars
 * @description 针对表【im_group_member(群成员表，记录群聊中成员的相关信息)】的数据库操作Service
 * @Date: 2025/3/6 22:32
 */
@Tag(name = "群成员")
@RequiredArgsConstructor
@RestController
@RequestMapping("/im/ban/record")
public class ImGroupMemberController extends BaseController {

    private final ImGroupMemberService imGroupMemberService;

    /**
     * 群添加成员
     */
    @RepeatSubmit(interval = 2, timeUnit = TimeUnit.SECONDS, message = "{repeat.submit.message}")
    @Operation(summary = "群添加成员")
    @PostMapping("/add")
    public R<?> addGroupMember(@RequestBody AddGroupMemberDTO addGroupMemberDTO){
        if (addGroupMemberDTO.getGroupId() == null){
            return R.fail("群编号不能为空");
        }
        return imGroupMemberService.addGroupMember(addGroupMemberDTO);
    }

    /**
     * 根据群id和用户id查询群用户
     */
    @Operation(summary = "根据群id和用户id查询群用户")
    @GetMapping("/query")
    public R<?> queryGroupMember(@RequestParam Long groupId, @RequestParam Long userId) {
        if (groupId == null || userId == null){
            return R.fail("群编号和用户编号不能为空");
        }
        return R.ok(imGroupMemberService.queryGroupMember(groupId, userId));
    }

    /**
     * 根据群id和用户id查询基本信息
     */
    @Operation(summary = "根据群id和用户id查询基本信息")
    @GetMapping("/query/base")
    public R<?> queryGroupMemberBase(@RequestParam Long groupId, @RequestParam Long userId) {
        if (groupId == null || userId == null){
            return R.fail("群编号和用户编号不能为空");
        }
        return R.ok(imGroupMemberService.handleGroupNicknameAndAvatar(groupId, userId));
    }
}
